package org.robot.work.into.oalib;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.jsoup.select.Elements;
import org.robot.Spider;
import org.robot.component.Attachment;
import org.robot.component.Page;
import org.robot.utils.RegexUtil;
import org.robot.utils.XmlUtil;

public class Bjmu extends Spider {
	private static final Long TASKID = 1001L;
	private static List<Attachment> list = Collections.synchronizedList(new LinkedList<>());

	@Override
	public void parse(Page page) throws Exception {
		if (page.isSeed()) {
			parseJournal(page);
			page.setNextLinksFilter(e -> RegexUtil.getYear(e.parent().text()).matches("201[5-9]"));
		} else if (page.typeEquals("issue")) {
			parseIssue(page);
		} else if (page.typeEquals("paper")) {
			parsePaper(page);
		}
		
	}
	
	private void parsePaper(Page page) {
		String title = joinStr(page, "h3.abs-tit");
		String authors = joinStr(page, "p[data-toggle=collapse] span");
		String affs = joinStr(page, "address.address ol li");
		Elements keyEles1 = page.select("strong:contains(关键词)");
		String c_keywords = null;
		if (!keyEles1.isEmpty()) {
			c_keywords = keyEles1.first().parent().select("a").text();
		}
		Elements keyEles = page.select("strong:contains(Key words)");
		String e_keywords = null;
		if (!keyEles.isEmpty()) {
			e_keywords = keyEles.first().parent().select("a").text();
		}
		String keywords = String.join("<br>", c_keywords, e_keywords);
		
		Elements abstEles1 = page.select("strong:contains(摘要)");
		String c_abst = null;
		if (!abstEles1.isEmpty()) {
			c_abst = abstEles1.first().parent().ownText();
		}
		Elements abstEles = page.select("strong:contains(Abstract)");
		String e_abst = null;
		if (!abstEles.isEmpty()) {
			e_abst = abstEles.first().parent().ownText();
		}
		String abst = String.join("<br>", c_abst, e_abst);
		String pages = (String) page.get("pages");
		String[] pageArr = pages.split("-");
		
		page.put("title", title);
		page.put("authorName", authors);
		page.put("keywords", keywords);
		page.put("paperAbstract", abst);
		page.put("fileName", "");
		page.put("fileUrl", "");
		page.put("imageFileName", "");
		page.put("doi", page.get("doi"));
		page.put("affs", affs);
		page.put("startPage", pageArr[0]);
		page.put("endPage", pageArr[1]);
		List<String> references = new ArrayList<>();
		page.put("references", references);
		list.add(page.getAttachment());
		
//		Paper paper = new Paper();
//		paper.setWebsite(page.url());
//		paper.setTitle(title);
//		paper.setTaskid(TASKID);
//		paper.setDoi(doi);
//		paper.setAbstracts(abst);
//		paper.setPdfUrl(pdfUrl);
//		paper.setIssueId(issueid);
//		PaperDao.addPaper(paper);
	}


	private String joinStr(Page page, String selector) {
		List<String> list = page.select(selector).eachText();
		return String.join("<br>", list);
	}

	private void parseIssue(Page page) {
		String str = page.select("div.njq").text();
		String issueName = str.substring(0, str.indexOf("期")+1);
		page.put("issueName", issueName);
		page.setNextInfoMap(e -> {
			String text = e.parent().parent().text();
			String doi = text.substring(text.lastIndexOf(":"));
			String pages = RegexUtil.regexFirst("\\d+-\\d+", text);
			Map<String, Object> map = page.createMap("doi", doi);
			map.put("pages", pages);
			return map;
		});
		
		System.out.println("journalename---" + page.prevAttachment().get("fullEnglishName"));
		
//		Long journalid = JournalDao.selectPidByWebsite(Journal.class, page.prevUrl(), TASKID);
//		String year = (String) page.get("year");
//		String vol = RegexUtil.regexFirst("\\d+", page.urlText());
//		Issue issue = new Issue();
//		issue.setIssue("Vol " + vol);
//		issue.setYear(year);
//		issue.setTaskId(TASKID);
//		issue.setWebsite(page.url());
//		issue.setJournalId(journalid);
//		IssueDao.addIssue(issue);
	}

	private void parseJournal(Page page) {
		page.put("fullEnglishName", "Journal of Peking University（Health Sciences）");
		page.put("fullChineseName", "北京大学学报（医学版）");
		page.put("shortEnglishName", "");
		page.put("issnPrint", "1671-167X");
		page.put("issnOnline", "");
		page.setNextInfoMap(e -> page.createMap("pubYear", RegexUtil.getYear(e.parent().text())));
		
//		String text = page.select("caption").text();
//		List<String> issnList = RegexUtil.regex("\\w{4}-\\w{4}", text);
//		Journal journal = new Journal();
//		journal.setWebsite(page.url());
//		journal.setIssn(issnList.get(0));
//		journal.setIssnonline(issnList.get(1));
//		journal.setTaskId(TASKID);
//		journal.setJournalname(page.urlText());
//		JournalDao.addJournal(journal);
	}

	public static void main(String[] args) {
		Bjmu claw = new Bjmu();
		claw.setBaseDirectory("E:/webSpider");
		claw.addSeed("http://xuebao.bjmu.edu.cn/CN/article/showOldVolumn.do");
		claw.addRegex("http://xuebao.bjmu.edu.cn/CN/volumn/volumn_\\d+.shtml", "issue");
		claw.addSelector("a.biaoti", "paper");
		claw.start();
		
		claw.asXml();
	}


	private void asXml() {
		for (int i = 0; i < list.size(); i+=50) {
			int toIndex = i + 50 > list.size() ? list.size() : i + 50;
			List<Attachment> subList = list.subList(i, toIndex);
			XmlUtil.generateXml(subList, new File(this.getConfig().getBaseDirectory()+"/xml", getClass().getSimpleName()+i+".xml"));
		}
	}
}
